  clear all
% import cv.*
sigma = 4;
% namelist = dir("D:\Desktop\clusters\*.txt");
% l=length(namelist);
% % clusters={};
%  filename = {};
%  for i=1:l
%      filename{i}="D:\Desktop\clusters\"+namelist(i).name;
%      clusters{i}=load(filename{i});
% end
% 
% save ("Clusters.mat","clusters");
% save ("Sigma.mat","sigma");

%load("Clusters.mat");
% load("Sigma.mat");
load("origin.mat");
load("part.mat");
% origin = gpuArray(origin);
% part1 = gpuArray(part1);

disp("开始计算P");
clusters={origin,part};
[G,V,S,U,P]=CalculateP(clusters,sigma);
disp("P计算完毕");



match_point_index={}
disp("开始计算匹配点");
[row,col]=size(P);
max_count=0;
r1=-1;
r2=-1;
match_count_record=zeros(row,col);

for r=1:row
    for c=r+1:col
            match_count=0;
            p = P{r,c};
            [n1,n2]=size(p);
            
            match_flag=zeros(max(n1,n2),max(n1,n2));
            if(n1<n2)
                for i=1:n1
                        for j=1:n2
                                 if(i~=j&&p(i,j)==max(p(i,:)) && p(i,j)==max(p(:,j))&&match_flag(i,j)==0)
                                    match_count=match_count+1;
                                    match_point_index{match_count}=[clusters{r}(i,:),clusters{c}(j,:)];
                                    match_flag(i,j)=1;
                                    match_flag(j,i)=1;
                                 end
                        end  
                    end
                    if(match_count>max_count)
                        max_count=match_count;
                        r1=r;
                        r2=c;
                    end
            else
                     for i=1:n2
                        for j=1:n1
                                 if(i~=j&&p(j,i)==max(p(j,:)) && p(j,i)==max(p(:,i))&&match_flag(j,i)==0)
                                    match_count=match_count+1;
                                    match_point_index{match_count}=[clusters{r}(j,:),clusters{c}(i,:)];
                                    match_flag(j,i)=1;
                                    match_flag(i,j)=1;
                                 end
                        end  
                    end
                    if(match_count>max_count)
                        max_count=match_count;
                        r1=r;
                        r2=c;
                    end
            end
        
        match_count_record(r,c)=match_count;
        disp(["轮廓",num2str(r),"和轮廓",num2str(c),"之间的匹配点个数为",num2str(match_count)]);
    end
end



disp(["最匹配结果为轮廓",num2str(r1),"和轮廓",num2str(r2),"之间的匹配点个数为",num2str(match_count_record(r1,r2))]);
[n1,n2]=size(match_point_index);
cx1 = zeros(n2);
cy1 = zeros(n2);
cx2 = zeros(n2);
cy2 = zeros(n2);
for i=1:n2
    cx1(i)=match_point_index{i}(1);
    cy1(i)=match_point_index{i}(2);
    cx2(i)=match_point_index{i}(3);
    cy2(i)=match_point_index{i}(4);
end
% cluster1 = clusters{r1};
% cx1=cluster1(:,1);
% cy1=cluster1(:,2);
% cluster2 = clusters{r2};
% cx2=cluster2(:,1);
% cy2=cluster2(:,2);
% I1=imread('D:\Desktop\origin.png');
% I2=imread('D:\Desktop\part.png');

figure(1)
imshow('D:\Desktop\origin.png');
hold on
plot(cx1,cy1,'go','LineWidth',2,'MarkerEdgeColor','red','MarkerFaceColor','red','MarkerSize',3)
hold on 

figure(2)
imshow('D:\Desktop\part.png')
hold on
plot(cx2,cy2,'cs','LineWidth',2,'MarkerEdgeColor','red','MarkerFaceColor','red','MarkerSize',3)
hold on
axis([0 12 0 32])
grid on
% T=legend(['图像',num2str(r1)],['图像',num2str(r2)]);
% set(T,'Fontsize',12);      %设置图标字体大小

% 
% scatter(cx1,cy1,36,'red');
% scatter(cx2,cy2,36,'green');
% 
% for i=1:l
%     for j=1:l
%         if(i==j)
%             continue;
%         end
%         G{i,j}=CalculateG(clusters{i},clusters{j},sigma);
%         [u,s,v]=svd(G{i,j})
%         V{i,j}=v;
%         S{i,j}=s;
%         U{i,j}=u;
%         P{i,j}=u*eye(size(s))*v;
%     end
% end

   
% im=imread("D:\Desktop\sample.png");
%im = rgb2gray(im);
% im=double(im);
% [Gx,Gy]=gradient(im);
% G=sqrt(Gx.*Gx+Gy.*Gy)/255;
% im(im<255/255)=0;
% im(im>=255/255)=1;

% U=edge(im,'canny');
% [m,n]=size(U);
% aa = ones(n,4);
% imshow(im);
% for i=1:n
%     d= U(1,i);
%     [ii.jj]=size(d{1,1});
%     jj
%     if(jj<10)
%         continue;
%     end
% end

